package com.tj.jinzao.widgets;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.tj.jinzao.R;
import com.tj.jinzao.utils.Utils;

/**
 * 选择dialog
 * Created by wjy on 2018/6/7.
 */
public class PictureDialog extends Dialog implements View.OnClickListener {

    protected Activity mContext;

    private boolean mHideVideo = false;
    private boolean mHideImage = false;
    private int mMaxNumber = 1;

    public void setMaxNumber(int number) {
        this.mMaxNumber = number;
    }

    public void setHideImage(boolean hideImage) {
        this.mHideImage = hideImage;
    }

    public void setHideVideo(boolean hideVideo) {
        this.mHideVideo = hideVideo;
    }

    public PictureDialog(Activity context) {
        super(context, R.style.Theme_Dialog_From_Bottom);
        this.mContext = context;
        this.setCanceledOnTouchOutside(true);
        this.setCancelable(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_picture);
        initLayoutParams();
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_video).setOnClickListener(this);
        findViewById(R.id.tv_pic).setOnClickListener(this);
        if (mHideImage) {
            findViewById(R.id.tv_pic).setVisibility(View.GONE);
            findViewById(R.id.line).setVisibility(View.GONE);
        } else if (mHideVideo) {
            findViewById(R.id.tv_video).setVisibility(View.GONE);
            findViewById(R.id.line).setVisibility(View.GONE);
        }
    }

    private void initLayoutParams() {
        // 不能写在init()中
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        lp.width = dm.widthPixels;
        lp.height = Utils.dpToPx(mHideImage || mHideVideo ? 130 : 180, mContext.getResources());
        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_pic) {
            PictureSelector.create(mContext)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(mMaxNumber)
                    .compress(true)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        } else if (v.getId() == R.id.tv_video) {
            PictureSelector.create(mContext)
                    .openGallery(PictureMimeType.ofVideo())
                    .maxSelectNum(1)
                    .compress(true)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }
        dismiss();
    }

}
